aux_source_directory(src SHELL_SRC)

add_executable(shell ${SHELL_SRC} ../src/pub.c)

IF(TD_LINUX_64 AND JEMALLOC_ENABLED)
    ADD_DEFINITIONS(-DTD_JEMALLOC_ENABLED -I${CMAKE_BINARY_DIR}/build/include -L${CMAKE_BINARY_DIR}/build/lib -Wl,-rpath,${CMAKE_BINARY_DIR}/build/lib -ljemalloc)
    SET(LINK_JEMALLOC "-L${CMAKE_BINARY_DIR}/build/lib -ljemalloc")
    ADD_DEPENDENCIES(shell jemalloc)
ELSE()
    SET(LINK_JEMALLOC "")
ENDIF()

IF(TD_LINUX AND TD_ALPINE)
    SET(LINK_ARGP "/usr/lib/libargp.a")
ELSE()
    SET(LINK_ARGP "")
ENDIF()

if(TD_WINDOWS)
    target_link_libraries(shell PUBLIC ${TAOS_LIB})
else()
    target_link_libraries(shell PUBLIC ${TAOS_LIB} ${LINK_JEMALLOC} ${LINK_ARGP})
endif()

target_link_libraries(
    shell
    PUBLIC ${LINK_WEBSOCKET}
    PRIVATE os common transport geometry util
)

IF(TD_WEBSOCKET)
    ADD_DEPENDENCIES(shell ext_taosws)
ENDIF()

target_include_directories(
    shell
    PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc"
)

# tweaking pitfalls found on Windows
# both taos.dll and taos.exe targets will generate taos.lib in ${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}
# this makes some targets, such as tmq_taosx_ci, which rely on taos.dll,
# might link with the wrong taos.lib generated by taos.exe target in concurrent building environment
# to tweak this, we choose taosql as the name of this shell target
# and add a post-build-command to copy it back to taos.exe to maintain compatibility
# Attention: do NOT delete taos.exe manually!!!
SET_TARGET_PROPERTIES(shell PROPERTIES OUTPUT_NAME taosql)
add_custom_command(TARGET shell
    POST_BUILD
    COMMAND "${CMAKE_COMMAND}" -E copy_if_different $<TARGET_FILE:shell> $<TARGET_FILE_DIR:shell>/taos$<TARGET_FILE_SUFFIX:shell>
    COMMENT "Copying taosql.exe to taos.exe"
)

#
# generator library shell_ut for uint test
#
IF(TD_LINUX)
    # include
    include_directories(${CMAKE_CURRENT_SOURCE_DIR}/inc)

    # shell_ut library
    add_library(shell_ut STATIC ${SHELL_SRC} ../src/pub.c)

    target_link_libraries(shell_ut PUBLIC ${TAOS_LIB} ${LINK_WEBSOCKET} ${LINK_JEMALLOC} ${LINK_ARGP})
    target_link_libraries(shell_ut PRIVATE os common transport geometry util)

    # unit test
    IF(${BUILD_TEST})
        ADD_SUBDIRECTORY(test)
    ENDIF(${BUILD_TEST})
ENDIF()

#
# collect --version information
#
MESSAGE("collect --version show info:")
# version
IF (DEFINED TD_VER_NUMBER)
    # TODO: global compile flags???
    ADD_DEFINITIONS(-DTD_VER_NUMBER="${TD_VER_NUMBER}")
    MESSAGE(STATUS "taos version:${TD_VER_NUMBER}")
ELSE ()
    # abort build
    MESSAGE(FATAL_ERROR "build taos not found TD_VER_NUMBER define.")
ENDIF ()
